const pool = require('../conf/db')

// 获取所有用户
const getAllUsers = async () => {
  const connection = await pool.getConnection()
  try {
    const [rows] = await connection.query('SELECT * FROM users')
    return rows
  } catch (err) {
    throw new Error('无法获取用户' + err)
  } finally {
    connection.release()
  }
}


// 查询某个用户
const getUserByParam = async (params) => {
  const connection = await pool.getConnection()
  try {
    const { key, value } = params
    const [rows] = await connection.query(`SELECT * FROM users WHERE ${key} = '${value}'`)
    return rows
  } catch (err) {
    throw new Error('查询用户失败' + err)
  } finally {
    connection.release()
  }
}

module.exports = {
  getAllUsers,
  getUserByParam
}
